home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / src-server / xlisp / xlsubr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-04  |  5.7 KB  |  226 lines

  1. /* -*-C-*-
  2. ********************************************************************************
  3. *
  4. * File:         xlsubr.c
  5. * RCS:          $Header: xlsubr.c,v 1.4 91/03/24 22:25:33 mayer Exp $
  6. * Description:  xlisp builtin function support routines
  7. * Author:       David Michael Betz
  8. * Created:      
  9. * Modified:     Fri Oct  4 04:12:25 1991 (Niels Mayer) mayer@hplnpm
  10. * Language:     C
  11. * Package:      N/A
  12. * Status:       X11r5 contrib tape release
  13. *
  14. * WINTERP Copyright 1989, 1990, 1991 Hewlett-Packard Company (by Niels Mayer).
  15. * XLISP version 2.1, Copyright (c) 1989, by David Betz.
  16. *
  17. * Permission to use, copy, modify, distribute, and sell this software and its
  18. * documentation for any purpose is hereby granted without fee, provided that
  19. * the above copyright notice appear in all copies and that both that
  20. * copyright notice and this permission notice appear in supporting
  21. * documentation, and that the name of Hewlett-Packard and David Betz not be
  22. * used in advertising or publicity pertaining to distribution of the software
  23. * without specific, written prior permission.  Hewlett-Packard and David Betz
  24. * make no representations about the suitability of this software for any
  25. * purpose. It is provided "as is" without express or implied warranty.
  26. *
  27. * HEWLETT-PACKARD AND DAVID BETZ DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  28. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  29. * IN NO EVENT SHALL HEWLETT-PACKARD NOR DAVID BETZ BE LIABLE FOR ANY SPECIAL,
  30. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  31. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  32. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  33. * PERFORMANCE OF THIS SOFTWARE.
  34. *
  35. * See ./winterp/COPYRIGHT for information on contacting the authors.
  36. * Please send modifications, improvements and bugfixes to mayer@hplabs.hp.com
  37. * Post XLISP-specific questions/information to the newsgroup comp.lang.lisp.x
  38. *
  39. ********************************************************************************
  40. */
  41. static char rcs_identity[] = "@(#)$Header: xlsubr.c,v 1.4 91/03/24 22:25:33 mayer Exp $";
  42.  
  43.  
  44. #include "xlisp.h"
  45.  
  46. /* external variables */
  47. extern LVAL k_test,k_tnot,s_eql;
  48.  
  49. /* xlsubr - define a builtin function */
  50. LVAL xlsubr(sname,type,fcn,offset)
  51.   char *sname; int type; LVAL (*fcn)(); int offset;
  52. {
  53.     LVAL sym;
  54.     sym = xlenter(sname);
  55.     setfunction(sym,cvsubr(fcn,type,offset));
  56.     return (sym);
  57. }
  58.  
  59. /* xlgetkeyarg - get a keyword argument */
  60. int xlgetkeyarg(key,pval)
  61.   LVAL key,*pval;
  62. {
  63.     LVAL *argv=xlargv;
  64.     int argc=xlargc;
  65.     for (argv = xlargv, argc = xlargc; (argc -= 2) >= 0; argv += 2) {
  66.     if (*argv == key) {
  67.         *pval = *++argv;
  68.         return (TRUE);
  69.     }
  70.     }
  71.     return (FALSE);
  72. }
  73.  
  74. /* xlgkfixnum - get a fixnum keyword argument */
  75. int xlgkfixnum(key,pval)
  76.   LVAL key,*pval;
  77. {
  78.     if (xlgetkeyarg(key,pval)) {
  79.     if (!fixp(*pval))
  80.         xlbadtype(*pval);
  81.     return (TRUE);
  82.     }
  83.     return (FALSE);
  84. }
  85.  
  86. /* xltest - get the :test or :test-not keyword argument */
  87. xltest(pfcn,ptresult)
  88.   LVAL *pfcn; int *ptresult;
  89. {
  90.     if (xlgetkeyarg(k_test,pfcn))    /* :test */
  91.     *ptresult = TRUE;
  92.     else if (xlgetkeyarg(k_tnot,pfcn))    /* :test-not */
  93.     *ptresult = FALSE;
  94.     else {
  95.     *pfcn = getfunction(s_eql);
  96.     *ptresult = TRUE;
  97.     }
  98. }
  99.  
  100. /* xlgetfile - get a file or stream */
  101. LVAL xlgetfile()
  102. {
  103.     LVAL arg;
  104.  
  105.     /* get a file or stream (cons) or nil */
  106.     if (arg = xlgetarg()) {
  107.     if (streamp(arg)) {
  108.         if (getfile(arg) == NULL)
  109.         xlfail("file not open");
  110.     }
  111.     else if (!ustreamp(arg))
  112.         xlerror("bad argument type",arg);
  113.     }
  114.     return (arg);
  115. }
  116.  
  117. /* xlgetfname - get a filename */
  118. LVAL xlgetfname()
  119. {
  120.     LVAL name;
  121.  
  122.     /* get the next argument */
  123.     name = xlgetarg();
  124.  
  125.     /* get the filename string */
  126.     if (symbolp(name))
  127.     name = getpname(name);
  128.     else if (!stringp(name))
  129.     xlerror("bad argument type",name);
  130.  
  131.     /* return the name */
  132.     return (name);
  133. }
  134.  
  135. /* needsextension - check if a filename needs an extension */
  136. int needsextension(name)
  137.   char *name;
  138. {
  139.     char *p;
  140.  
  141.     /* check for an extension */
  142.     for (p = &name[strlen(name)]; --p >= &name[0]; )
  143.     if (*p == '.')
  144.         return (FALSE);
  145.     else if (!islower(*p) && !isupper(*p) && !isdigit(*p))
  146.         return (TRUE);
  147.  
  148.     /* no extension found */
  149.     return (TRUE);
  150. }
  151.  
  152. /* xlbadtype - report a "bad argument type" error */
  153. LVAL xlbadtype(arg)
  154.   LVAL arg;
  155. {
  156.     xlerror("bad argument type",arg);
  157. }
  158.  
  159. /* xltoofew - report a "too few arguments" error */
  160. LVAL xltoofew()
  161. {
  162.     xlfail("too few arguments");
  163. }
  164.  
  165. /* xltoomany - report a "too many arguments" error */
  166. xltoomany()
  167. {
  168.     xlfail("too many arguments");
  169. }
  170.  
  171. /* eq - internal eq function */
  172. int eq(arg1,arg2)
  173.   LVAL arg1,arg2;
  174. {
  175.     return (arg1 == arg2);
  176. }
  177.  
  178. /* eql - internal eql function */
  179. int eql(arg1,arg2)
  180.   LVAL arg1,arg2;
  181. {
  182.     /* compare the arguments */
  183.     if (arg1 == arg2)
  184.     return (TRUE);
  185.     else if (arg1) {
  186.     switch (ntype(arg1)) {
  187.     case FIXNUM:
  188.         return (fixp(arg2) ? getfixnum(arg1)==getfixnum(arg2) : FALSE);
  189.     case FLONUM:
  190.         return (floatp(arg2) ? getflonum(arg1)==getflonum(arg2) : FALSE);
  191.     default:
  192.         return (FALSE);
  193.     }
  194.     }
  195.     else
  196.     return (FALSE);
  197. }
  198.  
  199. /* equal - internal equal function */
  200. int equal(arg1,arg2)
  201.   LVAL arg1,arg2;
  202. {
  203.     /* compare the arguments */
  204.     if (arg1 == arg2)
  205.     return (TRUE);
  206.     else if (arg1) {
  207.     switch (ntype(arg1)) {
  208.     case FIXNUM:
  209.         return (fixp(arg2) ? getfixnum(arg1)==getfixnum(arg2) : FALSE);
  210.     case FLONUM:
  211.         return (floatp(arg2) ? getflonum(arg1)==getflonum(arg2) : FALSE);
  212.     case STRING:
  213.         return (stringp(arg2) ? strcmp(getstring(arg1),
  214.                        getstring(arg2)) == 0 : FALSE);
  215.     case CONS:
  216.         return (consp(arg2) ? equal(car(arg1),car(arg2))
  217.                    && equal(cdr(arg1),cdr(arg2)) : FALSE);
  218.     default:
  219.         return (FALSE);
  220.     }
  221.     }
  222.     else
  223.     return (FALSE);
  224. }
  225.